package com.zy.mc_module.utils;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;

/**
 * Created on 18/11/1
 * 尺寸工具类
 */
public class SizeUtils {

    private SizeUtils() {
        throw new UnsupportedOperationException("不能实例化");
    }

    /**
     * dp转px
     *
     * @param context 上下文
     * @param dpValue dp值
     * @return px值
     */
    public static int dp2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    /**
     * px转dp
     *
     * @param context 上下文
     * @param pxValue px值
     * @return dp值
     */
    public static int px2dp(Context context, float pxValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (pxValue / scale + 0.5f);
    }

    /**
     * sp转px
     *
     * @param context 上下文
     * @param spValue sp值
     * @return px值
     */
    public static int sp2px(Context context, float spValue) {
        final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
        return (int) (spValue * fontScale + 0.5f);
    }

    /**
     * px转sp
     *
     * @param context 上下文
     * @param pxValue px值
     * @return sp值
     */
    public static int px2sp(Context context, float pxValue) {
        final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
        return (int) (pxValue / fontScale + 0.5f);
    }

    /***
     * 重设控件高度
     */
    public static void setHeight(View view, int height) {
        ViewGroup.LayoutParams params = view.getLayoutParams();
        params.height = height;
        view.setLayoutParams(params);
    }

    /***
     * 重设控件高度
     * 是屏幕高德几分之几
     */
    public static void setHeightScreen(Context context, View view, float multiple) {
        ViewGroup.LayoutParams params = view.getLayoutParams();
        params.height = (int) (ScreenUtils.getScreenHeight(context) * multiple);
        view.setLayoutParams(params);
    }

    /***
     * 重设控件宽度
     */
    public static void setWidth(View view, int width) {
        ViewGroup.LayoutParams params = view.getLayoutParams();
        params.width = width;
        view.setLayoutParams(params);
    }
}
